#if UNITY_EDITOR
using Sirenix.OdinInspector;
using Sirenix.Utilities;
using Sirenix.Utilities.Editor;
using UnityEditor;
using UnityEngine;

namespace AByte.UKit.Editor
{

    /*
     * 修改场景中选中对象的对象名称
     * 需求，打开面板的时，选中车型资源文件，绘制数据
     * 
     * 
     */
    public class ModifyGameObjectNameWin : HandTypeBaseWin
    {
        [MenuItem("UKit/修改场景对象名")]
        private static void Open()
        {
            var EditorWin = GetWindow<ModifyGameObjectNameWin>(title: "修改场景对象名");
            EditorWin.position = GUIHelper.GetEditorWindowRect().AlignCenter(600, 200);
        }

        protected override bool IsSelectedObject => Selection.gameObjects.Length > 0;
        
        
        #region 编号
        
        [Title("选择一个对象，对其激活的子对象进行编号")]
        [LabelText("编号位置")]
        [ShowIf("handleType", HandleType.Number),EnumToggleButtons]
        public NumberPos numberPos;
        
        [LabelText("原字符串头（尾）部删除的长度", true)]
        [ShowIf("handleType", HandleType.Number)]
        public int  numDelLen;
        
        [LabelText("编号长度，如3：编号为1时，001", true)]
        [ShowIf("handleType", HandleType.Number)]
        public int numFormatLen=2;
        [LabelText("起始编号", true)]
        [ShowIf("handleType", HandleType.Number)]
        public int startNum =1;
       
        [ShowIf("handleType", HandleType.Number)]
        [LabelText("编号"), LabelWidth(60)]
        [ShowInInspector,Button(ButtonSizes.Medium)]
        private void NumberHandler()
        {
            if (IsSelectedObject == false) return;
            if (numDelLen < 0) 
            {
                this.ShowNotification(new GUIContent { text = "被删除的长度不能为负数！" });
                return;
            }
            if (numFormatLen < 1) 
            {
                this.ShowNotification(new GUIContent { text = "编号长度必须大于0！" });
                return;
            }
            if (Selection.gameObjects .Length> 1) 
            {
                this.ShowNotification(new GUIContent { text = "智能选择一个对象！" });
                return;
            }
            TailNumberCmd();
        }

        #endregion
        
        protected override void DeleteCmd()
        {
            foreach (var item in Selection.gameObjects)
            {
                if ( delStartIndex+ delLen<=item.name.Length)
                {
                    item.name =  item.name.Remove(delStartIndex, delLen);
                }
            }
            this.ShowNotification(new GUIContent { text = "更新完成！" });
        }
        

        protected override void ModifyPrefixCmd(string oldPrefix, string newPrefix)
        {
            foreach (var item in Selection.gameObjects)
            {
                if (newPrefix != null) newPrefix.Trim();
                if (oldPrefix == null)
                {
                    item.name = newPrefix + item.name;
                }
                else
                {
                    if (item.name.StartsWith(oldPrefix))
                    {
                        item.name = newPrefix + item.name.Remove(0, oldPrefix.Length);
                    }
                }

            }
            this.ShowNotification(new GUIContent { text = "更新完成！" });
        }


        protected override void ModifySuffixCmd(string oldSuffix, string newSuffix)
        {
            foreach (var item in Selection.gameObjects)
            {

                if (newSuffix != null) newSuffix.Trim();
                if (oldSuffix == null)
                {
                    item.name = item.name + newSuffix;
                }
                else
                {
                    if (item.name.EndsWith(oldSuffix))
                    {
                        item.name = item.name.Remove(item.name.Length - oldSuffix.Length, oldSuffix.Length) + newSuffix;
                    }
                }
            }
            this.ShowNotification(new GUIContent { text = "更新完成！" });
        }



        protected override void ReplaceCmd(string replace_oldString, string replace_newString)
        {
            if (string.IsNullOrEmpty(replace_oldString))
            {
                this.ShowNotification(new GUIContent { text = "被替换的字符不能为空！" });
                return;
            }
            int count = 0;
            foreach (var item in Selection.gameObjects)
            {
                if (item.name.Contains(replace_oldString))
                {
                    item.name = item.name.Replace(replace_oldString, replace_newString);
                    count++;
                }
            }
            string info = $"共 {count} 个对象的名称被替换完成！";
            Debug.Log(info);
            this.ShowNotification(new GUIContent { text = info });
        }

        protected override void TailNumberCmd()
        {
            int count = 0;
            Transform tran = Selection.gameObjects[0].transform;
            if (numberPos == NumberPos.Header)
            {
                for (int i = 0; i < tran.childCount; i++)
                {
                    var item = tran.GetChild(i).gameObject;
                    if(item.activeSelf== false)continue;
                    if (item.name.Length>=numDelLen)
                    {
                        item.name = (startNum+count).ToString($"D{numFormatLen}")+ item.name.Substring(numDelLen);
                        count++;
                    }
                }
            }
            else
            {
                for (int i = 0; i < tran.childCount; i++)
                {
                    var item = tran.GetChild(i).gameObject;
                    if(item.activeSelf== false)continue;
                    if (item.name.Length>=numDelLen)
                    {
                        item.name = item.name.Substring(0,item.name.Length - numDelLen)+ (startNum+count).ToString($"D{numFormatLen}");
                        count++;
                    }

                }
            }
       
            string info = $"共 {count} 个对象的名称被替换完成！";
            Debug.Log(info);
            this.ShowNotification(new GUIContent { text = info });
        }
    }
}

#endif